﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;



namespace WindowsFormsApplication1
{
    

    public partial class AnimationEditor : Form
    {
     

        SGP.ManagedDirect3D d3D = SGP.ManagedDirect3D.Instance;
        SGP.ManagedTextureManager tm = SGP.ManagedTextureManager.Instance;

        bool looping = true;

        public bool Looping1
        {
            get { return looping; }
            set { looping = value; }
        }

        Frame tempFrame = new Frame();

        Animation tempAnimation = new Animation();

        List<Animation> tempListOfAnimations = new List<Animation>();

        List<Frame> tempListOfFrames = new List<Frame>();

        bool IsRenderRectActive = false;
        bool IsActiveRectActive = false;
        bool IsPassiveRectActive = false;
        bool IsAnchorPointActive = false;
        bool IsMoveActive = false;
        bool IsZoomActive = false;


        bool DoesRenderRectExsist = false;
        bool DoesActiveRectExsist = false;
        bool DoesPasiveRectExsist = false;
        bool DoesAnchorPointExsist = false;
        bool Looping = false;

        Tools tool = null;

        Bitmap SpriteSheet = Properties.Resources._default;

        public AnimationEditor()
        {
            InitializeComponent();
            looping = true;
            d3D.InitManagedDirect3D(RenderSpriteSheetPictureBox);

            tm.InitManagedTextureManager(d3D.Device, d3D.Sprite);

            //Graphics g = RenderSpriteSheetPanel.CreateGraphics();
            //SpriteSheet.SetResolution(g.DpiX, g.DpiY);

            //RenderSpriteSheetPanel.AutoScrollMinSize = SpriteSheet.Size;
      
        }

        public void Render()
        {
            d3D.Clear(RenderSpriteSheetPictureBox, Color.Beige);

       

            d3D.DeviceBegin();
            d3D.SpriteBegin();

            ////
          //  Point offset = new Point(0, 0);
            //    offset.X = RenderSpriteSheetPictureBox.AutoScrollPosition.X;
            //    offset.Y = RenderSpriteSheetPictureBox.AutoScrollPosition.Y;
         //   e.Graphics.DrawImage(SpriteSheet, offset);
           // Rectangle rect = RenderSpriteSheetPictureBox.ClientRectangle;
          //  Pen pen = new Pen(Color.Red);
           // pen.Width = 10;
          //  e.Graphics.DrawRectangle(pen, tempFrame.ActiveRect1);
          //  pen.Color = Color.ForestGreen;
          //  e.Graphics.DrawRectangle(pen, tempFrame.PassiveRect1);
          //  pen.Color = Color.Blue;
          //  e.Graphics.DrawRectangle(pen, tempFrame.RenderRect1);
          //  pen.Color = Color.DarkRed;
           
            //need draw elipse draw text can work i guess
            ////e.Graphics.DrawEllipse(pen, tempFrame.AcnchorPoint1.X, tempFrame.AcnchorPoint1.Y, 5, 5);

            d3D.DrawEmptyRect(tempFrame.ActiveRect1, Color.Red);
            d3D.DrawEmptyRect(tempFrame.PassiveRect1, Color.ForestGreen);
            d3D.DrawEmptyRect(tempFrame.RenderRect1, Color.Blue);

            d3D.DrawText("o", tempFrame.AcnchorPoint1.X, tempFrame.AcnchorPoint1.Y, Color.DarkGoldenrod);
            ////
            //start draw


            //end draw
            d3D.SpriteEnd();
            d3D.DeviceEnd();

            d3D.Present();
        }


        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            if (DialogResult.OK == dlg.ShowDialog())
            {


                Bitmap SpriteSheet = new Bitmap(dlg.FileName);

                Graphics g = RenderSpriteSheetPictureBox.CreateGraphics();
                SpriteSheet.SetResolution(g.DpiX, g.DpiY);

              //  RenderSpriteSheetPictureBox.AutoScrollMinSize = SpriteSheet.Size;

                RenderSpriteSheetPictureBox.Invalidate();

                if (tool == null)
                {
                    tool = new Tools();

                    tool.Show(this);
                }

            }


        }

 



        private void AnchorPointButton_Click(object sender, EventArgs e)
        {
            IsRenderRectActive = false;
            IsActiveRectActive = false;
            IsPassiveRectActive = false;
            IsAnchorPointActive = true;
            IsMoveActive = false;
            IsZoomActive = false;
        }

        private void RenderRectButton_Click(object sender, EventArgs e)
        {
            IsRenderRectActive = true;
            IsActiveRectActive = false;
            IsPassiveRectActive = false;
            IsAnchorPointActive = false;
            IsMoveActive = false;
            IsZoomActive = false;
        }

        private void PassiveRectButton_Click(object sender, EventArgs e)
        {
            IsRenderRectActive = false;
            IsActiveRectActive = false;
            IsPassiveRectActive = true;
            IsAnchorPointActive = false;
            IsMoveActive = false;
            IsZoomActive = false;
        }

        private void ActiveRectButton_Click(object sender, EventArgs e)
        {
            IsRenderRectActive = false;
            IsActiveRectActive = true;
            IsPassiveRectActive = false;
            IsAnchorPointActive = false;
            IsMoveActive = false;
            IsZoomActive = false;
        }

        private void ZoomButton_Click(object sender, EventArgs e)
        {
            IsRenderRectActive = false;
            IsActiveRectActive = false;
            IsPassiveRectActive = false;
            IsAnchorPointActive = false;
            IsMoveActive = false;
            IsZoomActive = true;

            
        }

        private void MoveButton_Click(object sender, EventArgs e)
        {
            IsRenderRectActive = false;
            IsActiveRectActive = false;
            IsPassiveRectActive = false;
            IsAnchorPointActive = false;
            IsMoveActive = true;
            IsZoomActive = false;
        }

        private void AnchorPointNumericUpDownX_ValueChanged(object sender, EventArgs e)
        {
            Point one = new Point(tempFrame.AcnchorPoint1.X, tempFrame.AcnchorPoint1.Y);
            one.X = (int)AnchorPointNumericUpDownX.Value;

            tempFrame.AcnchorPoint1 = one;
            RenderSpriteSheetPictureBox.Invalidate();

        }

        private void AnchorPointNumericUpDownY_ValueChanged(object sender, EventArgs e)
        {
            Point one = new Point(tempFrame.AcnchorPoint1.X, tempFrame.AcnchorPoint1.Y);
            one.Y = (int)AnchorPointNumericUpDownY.Value;

            tempFrame.AcnchorPoint1 = one;

            RenderSpriteSheetPictureBox.Invalidate();
        }

        private void RenderRectNumericUpDownX_ValueChanged(object sender, EventArgs e)
        {

            Rectangle one = new Rectangle(tempFrame.RenderRect1.X, tempFrame.RenderRect1.Y, tempFrame.RenderRect1.Width, tempFrame.RenderRect1.Height);
            one.X = (int)RenderRectNumericUpDownX.Value;

            tempFrame.RenderRect1 = one;

            RenderSpriteSheetPictureBox.Invalidate();
        }

        private void RenderRectNumericUpDownY_ValueChanged(object sender, EventArgs e)
        {
            Rectangle one = new Rectangle(tempFrame.RenderRect1.X, tempFrame.RenderRect1.Y, tempFrame.RenderRect1.Width, tempFrame.RenderRect1.Height);
            one.Y = (int)RenderRectNumericUpDownY.Value;

            tempFrame.RenderRect1 = one;

            RenderSpriteSheetPictureBox.Invalidate();
        }

        private void RenderRectNumericUpDownWidth_ValueChanged(object sender, EventArgs e)
        {
            Rectangle one = new Rectangle(tempFrame.RenderRect1.X, tempFrame.RenderRect1.Y, tempFrame.RenderRect1.Width, tempFrame.RenderRect1.Height);
            one.Width = (int)RenderRectNumericUpDownWidth.Value;

            tempFrame.RenderRect1 = one;

            RenderSpriteSheetPictureBox.Invalidate();
        }

        private void RenderRectNumericUpDownHeight_ValueChanged(object sender, EventArgs e)
        {
            Rectangle one = new Rectangle(tempFrame.RenderRect1.X, tempFrame.RenderRect1.Y, tempFrame.RenderRect1.Width, tempFrame.RenderRect1.Height);
            one.Height = (int)RenderRectNumericUpDownHeight.Value;

            tempFrame.RenderRect1 = one;

            RenderSpriteSheetPictureBox.Invalidate();
        }

        private void PassiveRectNumericUpDownX_ValueChanged(object sender, EventArgs e)
        {
            Rectangle one = new Rectangle(tempFrame.PassiveRect1.X, tempFrame.PassiveRect1.Y, tempFrame.PassiveRect1.Width, tempFrame.PassiveRect1.Height);
            one.X = (int)PassiveRectNumericUpDownX.Value;

            tempFrame.PassiveRect1 = one;

            RenderSpriteSheetPictureBox.Invalidate();
        }

        private void PassiveRectNumericUpDownY_ValueChanged(object sender, EventArgs e)
        {
            Rectangle one = new Rectangle(tempFrame.PassiveRect1.X, tempFrame.PassiveRect1.Y, tempFrame.PassiveRect1.Width, tempFrame.PassiveRect1.Height);
            one.Y = (int)PassiveRectNumericUpDownY.Value;

            tempFrame.PassiveRect1 = one;

            RenderSpriteSheetPictureBox.Invalidate();
        }

        private void PassiveRectNumericUpDownWidth_ValueChanged(object sender, EventArgs e)
        {
            Rectangle one = new Rectangle(tempFrame.PassiveRect1.X, tempFrame.PassiveRect1.Y, tempFrame.PassiveRect1.Width, tempFrame.PassiveRect1.Height);
            one.Width = (int)PassiveRectNumericUpDownWidth.Value;

            tempFrame.PassiveRect1 = one;

            RenderSpriteSheetPictureBox.Invalidate();
        }

        private void PassiveRectNumericUpDownHeight_ValueChanged(object sender, EventArgs e)
        {
            Rectangle one = new Rectangle(tempFrame.PassiveRect1.X, tempFrame.PassiveRect1.Y, tempFrame.PassiveRect1.Width, tempFrame.PassiveRect1.Height);
            one.Height = (int)PassiveRectNumericUpDownHeight.Value;

            tempFrame.PassiveRect1 = one;

            RenderSpriteSheetPictureBox.Invalidate();
        }

        private void ActiveRectNumericUpDownX_ValueChanged(object sender, EventArgs e)
        {
            Rectangle one = new Rectangle(tempFrame.ActiveRect1.X, tempFrame.ActiveRect1.Y, tempFrame.ActiveRect1.Width, tempFrame.ActiveRect1.Height);
            one.X = (int)ActiveRectNumericUpDownX.Value;

            tempFrame.ActiveRect1 = one;

            RenderSpriteSheetPictureBox.Invalidate();
        }

        private void ActiveRectNumericUpDownWidth_ValueChanged(object sender, EventArgs e)
        {
            Rectangle one = new Rectangle(tempFrame.ActiveRect1.X, tempFrame.ActiveRect1.Y, tempFrame.ActiveRect1.Width, tempFrame.ActiveRect1.Height);
            one.Width = (int)ActiveRectNumericUpDownWidth.Value;

            tempFrame.ActiveRect1 = one;

            RenderSpriteSheetPictureBox.Invalidate();
        }

        private void ActiveRectNumericUpDownY_ValueChanged(object sender, EventArgs e)
        {
            Rectangle one = new Rectangle(tempFrame.ActiveRect1.X, tempFrame.ActiveRect1.Y, tempFrame.ActiveRect1.Width, tempFrame.ActiveRect1.Height);
            one.Y = (int)ActiveRectNumericUpDownY.Value;

            tempFrame.ActiveRect1 = one;

            RenderSpriteSheetPictureBox.Invalidate();
        }

        private void ActiveRectNumericUpDownHeight_ValueChanged(object sender, EventArgs e)
        {
            Rectangle one = new Rectangle(tempFrame.ActiveRect1.X, tempFrame.ActiveRect1.Y, tempFrame.ActiveRect1.Width, tempFrame.ActiveRect1.Height);
            one.Height = (int)ActiveRectNumericUpDownHeight.Value;

            tempFrame.ActiveRect1 = one;

            RenderSpriteSheetPictureBox.Invalidate();
        }

        private void RenderSpriteSheetPictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                //  listOfFrames.Add(e.Location);

                if (IsRenderRectActive == true)
                {
                    RenderRectNumericUpDownX.Value = e.Location.X;
                    RenderRectNumericUpDownY.Value = e.Location.Y;

                    Rectangle one = new Rectangle(tempFrame.RenderRect1.X, tempFrame.RenderRect1.Y, tempFrame.RenderRect1.Width, tempFrame.RenderRect1.Height);
                    one.X = (int)RenderRectNumericUpDownX.Value;
                    one.Y = (int)RenderRectNumericUpDownY.Value;

                    tempFrame.RenderRect1 = one;

                }
                else if (IsActiveRectActive == true)
                {
                    ActiveRectNumericUpDownX.Value = e.Location.X;
                    ActiveRectNumericUpDownY.Value = e.Location.Y;

                    Rectangle one = new Rectangle(tempFrame.ActiveRect1.X, tempFrame.ActiveRect1.Y, tempFrame.ActiveRect1.Width, tempFrame.ActiveRect1.Height);
                    one.X = (int)ActiveRectNumericUpDownX.Value;
                    one.Y = (int)ActiveRectNumericUpDownY.Value;

                    tempFrame.ActiveRect1 = one;


                }
                else if (IsPassiveRectActive == true)
                {
                    PassiveRectNumericUpDownX.Value = e.Location.X;
                    PassiveRectNumericUpDownY.Value = e.Location.Y;

                    Rectangle one = new Rectangle(tempFrame.PassiveRect1.X, tempFrame.PassiveRect1.Y, tempFrame.PassiveRect1.Width, tempFrame.PassiveRect1.Height);
                    one.X = (int)PassiveRectNumericUpDownX.Value;
                    one.Y = (int)PassiveRectNumericUpDownY.Value;

                    tempFrame.PassiveRect1 = one;

                }
                else if (IsAnchorPointActive == true)
                {
                    AnchorPointNumericUpDownX.Value = e.Location.X;
                    AnchorPointNumericUpDownY.Value = e.Location.Y;

                    Point one = new Point(tempFrame.AcnchorPoint1.X, tempFrame.AcnchorPoint1.Y);
                    one.X = (int)AnchorPointNumericUpDownX.Value;
                    one.Y = (int)AnchorPointNumericUpDownY.Value;

                    tempFrame.AcnchorPoint1 = one;

                }


            }
        }

        private void RenderSpriteSheetPictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                //  listOfFrames.Add(e.Location);

                if (IsRenderRectActive == true)
                {
                    RenderRectNumericUpDownWidth.Value = e.Location.X - RenderRectNumericUpDownX.Value;
                    RenderRectNumericUpDownHeight.Value = e.Location.Y - RenderRectNumericUpDownY.Value;

                    Rectangle one = new Rectangle(tempFrame.RenderRect1.X, tempFrame.RenderRect1.Y, tempFrame.RenderRect1.Width, tempFrame.RenderRect1.Height);
                    one.Height = (int)RenderRectNumericUpDownHeight.Value;
                    one.Width = (int)RenderRectNumericUpDownWidth.Value;

                    tempFrame.RenderRect1 = one;


                }
                else if (IsActiveRectActive == true)
                {
                    ActiveRectNumericUpDownWidth.Value = e.Location.X - ActiveRectNumericUpDownX.Value;
                    ActiveRectNumericUpDownHeight.Value = e.Location.Y - ActiveRectNumericUpDownY.Value;


                    Rectangle one = new Rectangle(tempFrame.ActiveRect1.X, tempFrame.ActiveRect1.Y, tempFrame.ActiveRect1.Width, tempFrame.ActiveRect1.Height);
                    one.Height = (int)ActiveRectNumericUpDownHeight.Value;
                    one.Width = (int)ActiveRectNumericUpDownWidth.Value;


                    tempFrame.ActiveRect1 = one;
                }
                else if (IsPassiveRectActive == true)
                {
                    PassiveRectNumericUpDownWidth.Value = e.Location.X - PassiveRectNumericUpDownX.Value;
                    PassiveRectNumericUpDownHeight.Value = e.Location.Y - PassiveRectNumericUpDownY.Value;


                    Rectangle one = new Rectangle(tempFrame.PassiveRect1.X, tempFrame.PassiveRect1.Y, tempFrame.PassiveRect1.Width, tempFrame.PassiveRect1.Height);
                    one.Height = (int)PassiveRectNumericUpDownHeight.Value;
                    one.Width = (int)PassiveRectNumericUpDownWidth.Value;


                    tempFrame.PassiveRect1 = one;

                }


            }
        }

        private void AnimationEditor_FormClosing(object sender, FormClosingEventArgs e)
        {
            looping = false;
        }

      

    }
}
